package com.caiya.controller;

import com.caiya.cart.Res;
import com.caiya.cart.dto.cartinfo.CartAllListDto;
import com.caiya.cart.dto.cartinfo.CartInfoListDto;
import com.caiya.cart.entity.CartInfo;
import com.caiya.cart.vo.cartinfo.CartAllListVo;
import com.caiya.cart.vo.cartinfo.CartInfoAddVo;
import com.caiya.cart.vo.cartinfo.CartInfoListVo;
import com.caiya.cart.vo.cartinfo.CartInfoUpdateVo;
import com.caiya.service.CartInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by Administrator on 2018/12/3 0003.
 */
@RestController
@Api(description = "用户cart信息")
@RequestMapping("/api/cartinfo")
public class CartInfoController {

    @Autowired
    private CartInfoService cartInfoService;

    @PostMapping(value = "/alllist")
    @ApiOperation(value = "alllist",notes = "alllist")
    public Res allList(@RequestBody CartAllListVo cartAllListVo){
        Res res = null;
        try {
            PageInfo<CartAllListDto> list = cartInfoService.allList(cartAllListVo);
            res = new Res(list);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "list",notes = "")
    public Res list(@RequestBody CartInfoListVo cartInfoListVo){
        Res res = null;
        try {
            PageInfo<CartInfoListDto> info = cartInfoService.list(cartInfoListVo);
            res = new Res(info);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增",notes = "")
    public Res add(@RequestBody CartInfoAddVo cartInfoAddVo){
        Res res = null;
        try {
            int i = cartInfoService.add(cartInfoAddVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改",notes = "修改")
    public Res update(@RequestBody CartInfoUpdateVo cartInfoUpdateVo){
        Res res = null;
        try {
            int i = cartInfoService.update(cartInfoUpdateVo);
            res = new Res(i);
        }catch (Exception e){
            e.printStackTrace();
            res = new Res(Res.CODE_FAIL,e.getMessage());
        }
        return res;
    }
}
